﻿using System;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;

namespace ShaderPacker
{
    class Program
    {
        static void Main(string[] args)
        {
            var basePath = Environment.CurrentDirectory;
            if (args.Length > 0)
                basePath = args[0];
            var output = "xbim-shaders.js";
            if (args.Length > 1)
                output = args[1];
            var minify = args.Contains("-min");

            var shaders = Directory.EnumerateFiles(basePath, "*.c", SearchOption.TopDirectoryOnly).ToArray();
            if (shaders.Length == 0)
            {
                Console.WriteLine("There are no shaders in source folder.");
                Console.WriteLine("Usage:");
                Console.WriteLine("spacker [base directory] [output file] [-min]");
                Console.WriteLine("base directory: directory containing shaders in *.c files. All shader files will be packed into result file.");
                Console.WriteLine("output file: path and name of the result file. If the file exists it will be overwritten.");
                Console.WriteLine("-min : If you define -min switch result will be stripped of comments and blank characters.");
                Console.WriteLine();
                Console.WriteLine("Default values are:");
                Console.WriteLine("spacker .\\ xbim-shaders.js");
                Console.WriteLine();
                Console.ReadKey();
                return;
            }

            using (var tw = File.CreateText(Path.Combine(basePath, output)))
            {
                tw.WriteLine("/*");
                tw.WriteLine("* This file has been generated by spacker.exe utility. Do not change this file manualy as your changes");
                tw.WriteLine("* will get lost when the file is regenerated. Original content is located in *.c files.");
                tw.WriteLine("*/");

                //start object definition
                tw.WriteLine("if (!window.xShaders) window.xShaders = {}");

                //get content of every shader and create JS property containing code content of the shader
                foreach (var shader in shaders)
                {
                    var shaderName = Path.GetFileNameWithoutExtension(shader);
                    if (shaderName != null)
                    {
                        shaderName = shaderName.Replace('.', '_');
                        tw.Write("xShaders.{0} = {1}", shaderName, minify ? "\"" : "");
                    }

                    var lines = File.ReadAllLines(shader);
                    for (var i = 0; i < lines.Length; i++)
                    {
                        var line = lines[i];
                        var isLast = i == (lines.Length - 1);

                        if (minify)
                        {
                            //remove comments
                            var exp = new Regex("//.*");
                            var stripLine = exp.Replace(line, "").Trim();
                            if (!string.IsNullOrEmpty(stripLine))
                                tw.Write(" " + stripLine);
                        }
                        else
                            tw.WriteLine("\"{0} \\n \" {1}", line, isLast ? "" : "+");

                    }
                    tw.WriteLine(minify ? "\";" : ";");
                }

                tw.Close();
            }
        }
    }
}
